package com.atstudying.mall.product.controller;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.R;
import com.atstudying.common.valid.AddGroup;
import com.atstudying.common.valid.UpdateGroup;
import com.atstudying.common.valid.UpdateStatusGroup;
import com.atstudying.mall.product.entity.BrandEntity;
import com.atstudying.mall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 品牌
 *
 * @author zlj
 * @email 2908434305@qq.com
 * @date 2020-10-04 22:40:50
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
   // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    //不去感知错误的异常处理信息，那么遇到问题就会自动抛出去
    @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Validated(value = {AddGroup.class}) @RequestBody BrandEntity brand){
//        Map<String,String> map=new HashMap<>();
//
//        if (bindingResult.hasErrors()){
//            bindingResult.getFieldErrors().forEach((item)->{
//                String message= item.getDefaultMessage();
//                String field = item.getField();
//                map.put(field,message);
//            });
//            System.out.println(".......");
//            return R.error(400,"提交数据不合法").put("data",map);
//        }else {
//            brandService.save(brand);
//
//        }
        brandService.save(brand);
        return R.ok();

        }

    /**
     * 修改
     *      *      *      * @RequestBody：获取请求体，必须发送post请求（只有post请求才有请求体，get请求没有请求体）
     * springmvc（web)自动将请求体的数据（json），转化为对应的对象
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:brand:update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }
    //修改状态
    @RequestMapping("/update/status")
    // @RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated(value = {UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
